package com.zjw.hyphenateui.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



import com.tencent.imsdk.TIMConversation;
import com.tencent.imsdk.TIMFriendshipManager;
import com.tencent.imsdk.TIMMessage;
import com.tencent.imsdk.TIMUserProfile;
import com.tencent.imsdk.TIMValueCallBack;
import com.tencent.imsdk.ext.message.TIMConversationExt;
import com.tencent.imsdk.ext.message.TIMManagerExt;
import com.zjw.hyphenateui.R;
import com.zjw.hyphenateui.adapter.ConversationAdapter;
import com.zjw.hyphenateui.bean.MyConversation;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MessageFragment extends BaseFragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    Unbinder unbinder;
    private ConversationAdapter mAdapter;

    private  List<MyConversation> myConversationList = new ArrayList<>();
    private String TAG = "mango";


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_message;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onResume() {
        super.onResume();
        loadConversationList();
    }

    public void refresh() {
        //获取刷新会话列表
        mAdapter.clear();
        mAdapter.addAll(myConversationList);
    }

    //获取会话列表
    protected void loadConversationList(){
        myConversationList.clear();
        final List<TIMConversation> list = TIMManagerExt.getInstance().getConversationList();
        //待获取用户资料的用户列表
        List<String> users = new ArrayList<String>();
        for (TIMConversation conversation : list)
        {
            users.add(conversation.getPeer());
        }
        //获取用户资料
        TIMFriendshipManager.getInstance().getUsersProfile(users, new TIMValueCallBack<List<TIMUserProfile>>(){
            @Override
            public void onError(int code, String desc){
                //错误码 code 和错误描述 desc，可用于定位请求失败原因
                //错误码 code 列表请参见错误码表
                Log.e(TAG, "getUsersProfile failed: " + code + " desc");
            }
            @Override
            public void onSuccess(List<TIMUserProfile> result){
                Log.e(TAG, "getUsersProfile succ");

                for(int i = 0; i<result.size(); i++){
                    TIMUserProfile res = result.get(i);
                    Log.e(TAG, "identifier: " + res.getIdentifier() + " nickName: " + res.getNickName()
                            + " remark: " + res.getRemark());
                    MyConversation myConversation = new MyConversation();
                    myConversation.setIdentifier(res.getIdentifier());
                    TIMConversationExt conversationExt = new TIMConversationExt(list.get(i));
                    List<TIMMessage> lastMsgs = conversationExt.getLastMsgs(1);
                    myConversation.setUnReadCount(conversationExt.getUnreadMessageNum());
                    myConversation.setLastMessage(lastMsgs.get(0));
                    myConversation.setFaceUrl(res.getFaceUrl());
                    myConversationList.add(myConversation);
                }

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        refresh();
                    }
                });
            }
        });
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter = new ConversationAdapter(mContext));
        refresh();
        mAdapter.setOnItemRootClickListener(new ConversationAdapter.OnItemRootClickListener() {

            @Override
            public void onItemClick(View view, int position) {

            }
        });
    }

}
